﻿<Window x:Class="MartinCarolan.EventSpy.UI.MainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:conv="clr-namespace:MartinCarolan.EventSpy.UI.Converters"
             xmlns:ui="clr-namespace:MartinCarolan.EventSpy.UI"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             mc:Ignorable="d"
             Title="Martin Carolan's Event Spy"
             ResizeMode="CanResizeWithGrip"
             Icon="pack://application:,,,/MartinCarolan.EventSpy;Component/Resources/event.png"
             Closing="Window_Closing">
    <Window.Resources>
        <conv:ObjectToTypeNameConverter x:Key="typeNameConverter" />
        <conv:ListToCountConverter x:Key="countConverter" />
        <CollectionViewSource x:Key="eventsSource" Source="{Binding Path=Events}" Filter="CollectionViewSource_Filter" />
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="200" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="320" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        
        <StatusBar Grid.Row="3" Grid.ColumnSpan="3">
            <TextBlock>Martin Carolan's Event Spy</TextBlock>
            <Separator />
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="Spying on {0} events in {1} objects">
                        <Binding Path="Registry.Values" Converter="{StaticResource countConverter}" />
                        <Binding Path="Registry.Keys.Count" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
            <Separator />
            <TextBlock Text="{Binding Source={StaticResource eventsSource}, Path=Count, StringFormat='Showing {0} captured events'}" />
        </StatusBar>
        
        <ui:ObjectExplorer Grid.Row="2" Grid.ColumnSpan="3" Instance="{Binding ElementName=eventsGrid, Path=SelectedItem.ArgumentWrapper}" />

        <GridSplitter Grid.Row="1" Grid.ColumnSpan="3" Height="5" VerticalAlignment="Stretch" />
        
        <TreeView Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Path=Registry}" >
            <TreeView.ItemTemplate>
                <DataTemplate>
                    <TreeViewItem IsExpanded="True" ItemsSource="{Binding Path=Value}">
                        <TreeViewItem.Header>
                            <StackPanel Orientation="Horizontal">
                                <Image Margin="2" Source="pack://application:,,,/MartinCarolan.EventSpy;Component/Resources/object.png" />
                                <TextBlock Text="{Binding Path=Key, Converter={StaticResource typeNameConverter}}" />
                            </StackPanel>
                        </TreeViewItem.Header>
                        <TreeViewItem.ItemTemplate>
                            <HierarchicalDataTemplate>
                                <TreeViewItem>
                                    <TreeViewItem.Header>
                                        <CheckBox IsChecked="{Binding Path=ShowInGrid}" Unchecked="CheckBox_Checked" Checked="CheckBox_Checked">
                                            <StackPanel Orientation="Horizontal">
                                                <Image Margin="2" Source="pack://application:,,,/MartinCarolan.EventSpy;Component/Resources/event.png" />
                                                <TextBlock Text="{Binding Path=Event.Name}" />
                                            </StackPanel>
                                        </CheckBox>
                                    </TreeViewItem.Header>
                                </TreeViewItem>
                            </HierarchicalDataTemplate>
                        </TreeViewItem.ItemTemplate>
                    </TreeViewItem>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
        
        <GridSplitter Grid.Row="0" Grid.Column="1" Width="5" HorizontalAlignment="Stretch" />
        
        <DataGrid Grid.Row="0" Grid.Column="2" GridLinesVisibility="None" IsReadOnly="True"
                  CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="True"
                  CanUserResizeColumns="True" CanUserSortColumns="True"
                  ItemsSource="{Binding Source={StaticResource eventsSource}}"
                  AutoGenerateColumns="False"
                  x:Name="eventsGrid">
        	<DataGrid.HorizontalGridLinesBrush>
        		<SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlDarkColorKey}}"/>
        	</DataGrid.HorizontalGridLinesBrush>
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="16">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image Margin="2" Source="pack://application:,,,/MartinCarolan.EventSpy;Component/Resources/event.png" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="270" />
                <DataGridTextColumn Header="Source" CanUserSort="False" Binding="{Binding Path=Source, Converter={StaticResource typeNameConverter}}" Width="250" />
                <DataGridTextColumn Header="Time Raised" Binding="{Binding Path=TimeStamp, StringFormat='HH:mm:fffffff'}" Width="100" />
                <DataGridTextColumn Header="Relative Time" SortDirection="Descending" Binding="{Binding Path=RelativeTime}" Width="100" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
